//
//  SuperviseTaskCheckDetailView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/9.
//

import Foundation
import UIKit

class SuperviseTaskCheckDetailView: UIScrollView {
    let contentV = UIView()

    override init(frame: CGRect) {
        super.init(frame: frame)
        showsVerticalScrollIndicator = false
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModals modals: [SpRwbgjcModal?]) {
        contentV.removeAllSubViews()
        
        var lastI: SuperviseCheckView?
        for (index, modal) in modals.enumerated() {
            let type = SuperviseCheckingEnum(rawValue: (modal?.rwzt)!)
            let itemV = SuperviseCheckView(withType: type)
            itemV.reload(withModal: modal)
            contentV.addSubview(itemV)
            itemV.snp.makeConstraints { make in
                make.left.right.equalToSuperview()
                make.top.equalTo(lastI == nil ? contentV.snp.top : lastI!.snp.bottom).offset(10)
                if index == modals.count - 1 {
                    make.bottom.equalToSuperview()
                }
            }
            lastI = itemV
        }
    }
    
    func setupUI() {
        addSubview(contentV)
        contentV.snp.makeConstraints { (make) -> Void in
            make.top.left.equalToSuperview()
            make.width.equalTo(ScreenWidth)
            make.bottom.equalToSuperview().offset(-20)
        }
    }
}

class SuperviseCheckView: UIView {
    var isCheckType: SuperviseCheckingEnum?
    
    let passItem = SuperviseCheckItem(withType: .pass)
    let failItem = SuperviseCheckItem(withType: .fail)
    let skipItem = SuperviseCheckItem(withType: .skip)
    
    let contentV = UIView.createBase()
    
    let msgL = PaddingLabel()
    let collectionV = SEImageCollectionView()

    init(withType type: SuperviseCheckingEnum? = nil) {
        super.init(frame: .zero)
        setupUI()
        self.isCheckType = type
        updateEntry()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal modal: SpRwbgjcModal?) {
        titleL.text = modal?.rwmc
        msgL.text = modal?.fjsm
        
        var imgs: [String] = []
        let images = modal?.rwjlMbFjs ?? []
        for img in images {
            if img.fjdz != nil {
                imgs.append(img.fjdz!)
            }
        }
        collectionV.imgs = imgs
    }
    
    func updateEntry() {
        passItem.isCheck = isCheckType == passItem.type
        failItem.isCheck = isCheckType == failItem.type
        skipItem.isCheck = isCheckType == skipItem.type
        
        if isCheckType == .fail {
            addFailMsgView()
        } else {
            msgL.text = ""
            collectionV.imgs = []
            removeFailMsgView()
        }
    }
    
    func addFailMsgView() {
        contentV.layer.opacity = 1.0
        contentV.snp.remakeConstraints { make in
            make.left.equalToSuperview().offset(17)
            make.right.equalToSuperview().offset(-17)
            make.top.equalTo(passItem.snp.bottom).offset(8)
            make.bottom.equalToSuperview().offset(-18)
        }
    }
    
    func removeFailMsgView() {
        contentV.layer.opacity = 0.0
        contentV.snp.remakeConstraints { make in
            make.left.equalToSuperview().offset(17)
            make.right.equalToSuperview().offset(-17)
            make.top.equalTo(passItem.snp.bottom)
            make.height.equalTo(0)
            make.bottom.equalToSuperview().offset(-18)
        }
    }
    
    func setupUI() {
        let baseV = UIView.createBase()
        addSubview(baseV)
        baseV.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview()
        }
        
        baseV.addSubview(titleL)
        titleL.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.left.equalToSuperview().offset(17)
            make.right.equalToSuperview().offset(-17)
        }
        
        let itemW = (ScreenWidth - 95) / 3.0
        
        baseV.addSubview(passItem)
        passItem.snp.makeConstraints { make in
            make.top.equalTo(titleL.snp.bottom).offset(15)
            make.left.equalToSuperview().offset(17)
            make.width.equalTo(itemW)
            make.height.equalTo(65)
        }
        
        baseV.addSubview(failItem)
        failItem.snp.makeConstraints { make in
            make.left.equalTo(passItem.snp.right).offset(17)
            make.width.equalTo(itemW)
            make.height.equalTo(passItem.snp.height)
            make.top.equalTo(passItem.snp.top)
        }
        
        baseV.addSubview(skipItem)
        skipItem.snp.makeConstraints { make in
            make.left.equalTo(failItem.snp.right).offset(17)
            make.width.equalTo(itemW)
            make.height.equalTo(passItem.snp.height)
            make.top.equalTo(passItem.snp.top)
        }
        
        contentV.layer.opacity = 0.0
        contentV.backgroundColor = .bg
        contentV.layer.cornerRadius = 10
        contentV.layer.masksToBounds = true
        baseV.addSubview(contentV)
        contentV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(17)
            make.right.equalToSuperview().offset(-17)
            make.top.equalTo(passItem.snp.bottom)
            make.height.equalTo(0)
            make.bottom.equalToSuperview().offset(-18)
        }
        
        msgL.numberOfLines = 0
        msgL.backgroundColor = .white
        msgL.layer.masksToBounds = true
        msgL.layer.cornerRadius = 5
        msgL.layer.borderWidth = 0.5
        msgL.layer.borderColor = UIColor.hex("#DEDEDE").cgColor
        msgL.layer.backgroundColor = UIColor.white.cgColor
        msgL.font = UIFont.systemFont(ofSize: 13)
        msgL.textColor = .hex("#333333")
        msgL.insets = UIEdgeInsets(top: 10, left: 8, bottom: 10, right: 10)
        contentV.addSubview(msgL)
        msgL.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.top.equalToSuperview().offset(16)
        }

        collectionV.itemHeight = 65
        collectionV.maxCount = 4
        contentV.addSubview(collectionV)
        collectionV.snp.makeConstraints { (make) in
            make.top.equalTo(msgL.snp.bottom).offset(10)
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.bottom.equalTo(self.snp.bottom).offset(-20)
        }
    }
    
    lazy var titleL: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = .hex("#333333")
        label.numberOfLines = 0
        return label
    }()
}
